package com.fbs.service.impl;

import com.aaa.entity.EmpInfo;
import com.aaa.vo.EmpInfoVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fbs.dao.EmpInfoDao;
import com.fbs.service.EmpInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class EmpInfoServiceImpl implements EmpInfoService {
    @Autowired
    private EmpInfoDao empInfoDao;

    //分页查询
    @Override
    public Result findAll(String ename,Integer pageNum,Integer pageSize) {
        //根据分页信息和模糊查询字段查询
        List<EmpInfo> all = empInfoDao.findAll(ename,(pageNum-1)*pageSize,pageSize);
        if (ename==null){
            ename="";
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("ename",ename);
        //根据模糊查询查出total
        Long total = empInfoDao.selectCount(queryWrapper);
        //封装将查处的信息封装到map返回给客户端
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",all);
        map.put("total",total);
        return new Result(200,"查询成功",map);
    }

    @Override
    public Result insert(EmpInfo empInfo) {
        Date date = new Date();
        empInfo.setGmtCreate(date);
        empInfo.setIsDeleted(0);
        empInfo.setPassword("$2a$10$TSN.LepgqdkcA00VfBJjuudreVuoAkAHLCkkwwDHMM1Sbyjq7H16S");
        int insert = empInfoDao.insert(empInfo);
        if(insert!=0){
        return new Result(200,"插入成功");
        }
        return new Result(500,"插入失败");
    }

    @Override
    public Result delete(Integer id) {
        int i = empInfoDao.deleteById(id);
        if (i!=0){
        return new Result(200,"删除成功");
        }
        return new Result(500,"删除失败");
    }

    @Override
    public Result save(EmpInfo empInfo) {
        empInfo.setGmtModified(new Date());
        UpdateWrapper<EmpInfo> empInfoUpdateWrapper = new UpdateWrapper<>();
        empInfoUpdateWrapper.eq("id",empInfo.getId());
        int update = empInfoDao.update(empInfo, empInfoUpdateWrapper);

        if (update!=0){
            return new Result(200,"修改成功");
        }
        return new Result(500,"修改失败");
    }

}
